<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *添加会员信息
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'oneadd';
$merchid=$this->checklogin();
if($_RQ['op']=='oneadd'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['mobile']){
			exi('请输入手机号！','error');
		}
		$mid=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$postdata['mobile']),'id');
		if($mid){
			$check2mobile=pdo_getcolumn('jy_merch_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'merchid'=>$merchid),'mobile');
		}
		if($mid && $check2mobile){
			exi('该手机会员已存在！','error');
		}
		$member=array(
			'mobile'=>$postdata['mobile'],
			'nickname'=>$postdata['realname']?:$postdata['mobile'],
			'realname'=>$postdata['realname']?:$postdata['mobile'],
		);
		if(!$mid){
			$mid=cfc('member')->register($member,true);
			$update=array(
    			'sex'=>$postdata['sex'],
    			'birth'=>ijson_encode($postdata['birth']),
    			'avatar'=>'../core/resource/images/dwcaser.png'
    		);
			pdo_update('core_members',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid));
			//线上与线下会员信息的同步
        	//查询是否有线上会员
            //$xian=pdo_getcolumn('core_members',array('mobile'=>trim($postdata['mobile']),'uniacid'=>$_SESSION['uniacid'],'id <>'=>$_SESSION['uid']),'id');
            //$merch_user = pdo_get('jy_merch_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$xian,'merchid'=>$postdata['merchid']));
            //if($merch_user['hytype']==0){
                //pdo_update('jy_merch_member',array('mid'=>$_SESSION['uid'],'credit1 +='=>$merch_user['credit1'],'credit2 +='=>$merch_user['credit2']),array('id'=>$merch_user['id']));
                //pdo_update('core_members',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid));
            //}
		    
		
		}
		
		if(!$check2mobile){
			$this->model('member')->addmember($merchid,$mid);
		
			$muser = pdo_get('jy_merch_member',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'mid'=>$mid));
			$updata = array('hytype'=>1);
			if($postdata['hzid']){
			    $updata['hzid']  = $postdata['hzid'];
			    $updata['isdaan']  = 1;
			}
			pdo_update('jy_merch_member',$updata,array('id'=>$muser['id']));
			
			//新增商城会员信息
			$member = pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'merchid'=>$merchid));
			$insert = array('mid'=>$mid,'merchid'=>$merchid,'level'=>$postdata['groupid'],'uniacid'=>$_SESSION['uniacid']);
			if(!$member){
			    
			    pdo_insert('jy_weishop_member',$insert);
			}
		}
		exi('添加成功');
	}
	$return['item']=array();
	$return['years']=array();
	for($i=1914;$i<2100;$i++){
		$return['years'][]=$i;
	}
	$return['months']=array(1,2,3,4,5,6,7,8,9,10,11,12);
	$return['days']=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','title'=>'客户名称','name'=>'realname'),
			array('type'=>'string','title'=>'手机号','name'=>'mobile'),
			array('type'=>'select','title'=>'会员等级','name'=>'groupid','help'=>'','options'=>'groups'),
			array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
			array('type'=>'day','title'=>'生日','name'=>'birth','help'=>''),
			//关联患者档案
			array('name'=>'hzid','title'=>'关联患者档案','type'=>'choose','action'=>'jy_merch/manage.get_daan/')
		),
	);
	$return['sex']=array('未知','男','女');
	$return['groups']=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'));
	$return['htmls']=$htmls;
	
	exi($return);
}